﻿Imports System.Xml
Imports System.Xml.Xsl
Imports System.Text

Public Class XsltCompiled
    Private Shared mlockHelper As New Object()

    REM XSL解析XML数据,并返回结果
    Public Shared Function XmlTrans(ByVal XPathDoc As XPath.XPathDocument, ByVal XsltString As String) As String
        Return XmlTrans(XPathDoc, XsltString, Nothing)
    End Function

    Public Shared Function XmlTrans(ByVal XPathDoc As XPath.XPathDocument, ByVal XslArgList As XsltArgumentList, ByVal Uri As String) As String
        Return XmlTrans(XPathDoc, IO.File.ReadAllText(Uri), XslArgList, Uri, -1)
    End Function


    Public Shared Function XmlTrans(ByVal XPathDoc As XPath.XPathDocument, ByVal XsltString As String, ByVal XslArgList As XsltArgumentList, Optional ByVal Uri As String = "", Optional ByVal CacheTime As Integer = 20) As String

        Dim sb As New System.Text.StringBuilder
        Dim XslDoc As System.Xml.Xsl.XslCompiledTransform
        SyncLock mlockHelper
            Try
                If String.IsNullOrEmpty(Uri) OrElse System.Web.HttpContext.Current.Cache(Uri) Is Nothing Then
                    XslDoc = New System.Xml.Xsl.XslCompiledTransform(False)

                    Dim XmlResolver As New System.Xml.XmlUrlResolver
                    XmlResolver.Credentials = System.Net.CredentialCache.DefaultCredentials

                    Dim tmp As IO.TextReader = New IO.StringReader(XsltString)

                    Dim tmpXPath As New XPath.XPathDocument(tmp)

                    XslDoc.Load(tmpXPath, System.Xml.Xsl.XsltSettings.TrustedXslt, XmlResolver)

                    tmp.Close()
                    tmp = Nothing
                    tmpXPath = Nothing
                    REM 加入缓存
                    If Not String.IsNullOrEmpty(Uri) Then
                        Dim dep As New Web.Caching.CacheDependency(Uri, DateTime.Now)
                        If CacheTime = -1 Then
                            System.Web.HttpContext.Current.Cache.Insert(Uri, XslDoc, dep)
                        Else
                            System.Web.HttpContext.Current.Cache.Insert(Uri, XslDoc, dep, Web.Caching.Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(CacheTime))
                        End If
                        dep = Nothing
                    End If
                Else
                    XslDoc = CType(System.Web.HttpContext.Current.Cache(Uri), System.Xml.Xsl.XslCompiledTransform)
                End If

                'Dim XmlSet As New System.Xml.XmlWriterSettings
                'XmlSet.ConformanceLevel = System.Xml.ConformanceLevel.Fragment
                'XmlSet.DoNotEscapeUriAttributes = False
                'XmlSet.NewLineOnAttributes = False
                'XmlSet.NewLineChars = String.Empty 'vbCrLf
                'XmlSet.NewLineHandling = System.Xml.NewLineHandling.None

                'XmlSet.CheckCharacters = False
                'XmlSet.Indent = True

                'XmlSet.CloseOutput = True
                'XmlSet.OmitXmlDeclaration = False
                'If Not String.IsNullOrEmpty(Util.AppSettings("charset")) Then
                '    XslDoc.OutputSettings.Encoding = System.Text.Encoding.GetEncoding(Util.AppSettings("charset"))
                'End If
                Dim XmlWriter As System.Xml.XmlWriter = System.Xml.XmlWriter.Create(sb, XslDoc.OutputSettings)
                XslDoc.Transform(XPathDoc, XslArgList, XmlWriter)
                XmlWriter.Close()
                XmlWriter = Nothing
                'XmlSet = Nothing
                XslArgList = Nothing
            Catch ex As XsltCompileException
                sb.Append(String.Format("<font color='#FF0000'>(XmlTrans)Xslt Load Error:{0}</font>", ex.Message))
            Catch ex As XsltException
                sb.Append(String.Format("<font color='#FF0000'>(XmlTrans)Xslt Transform Error:{0}</font>", ex.Message))
            Catch ex As Exception
                sb.Append(String.Format("<font color='#FF0000'>(XmlTrans)Xslt Error:{0}</font>", ex.Message))
            End Try
        End SyncLock

        XslDoc = Nothing
        XPathDoc = Nothing
        Return sb.ToString

    End Function

End Class

